//
//  User.swift
//  RacWeibo
//
//  Created by zimu on 15/10/26.
//  Copyright © 2015年 hanxu. All rights reserved.
//

import UIKit
///用户账户
class UserAccount: NSObject, NSCoding{
    
    ///用户token
    var access_token : String?
    ///过期时间
    var expires_in : String?
    ///用户uid
    var uid : String?
    ///idstr
    var idstr : String?
    ///名称
    var name : String?
    ///头像
    var profile_image_url : String?
    ///地址
    var location : String?
    ///建立时间
    var created_at : String?
    
    /// 归档的路径
    private static let userSandBoxPath = NSSearchPathForDirectoriesInDomains(NSSearchPathDirectory.DocumentDirectory, NSSearchPathDomainMask.UserDomainMask, true).last!.stringByAppendingString("user.plist")

    //MARK: - 字典转模型
    init(dict: [String : AnyObject]) {
        super.init()

        setValuesForKeysWithDictionary(dict)
    }
    ///这个方法简直牛逼
    override func setValue(value: AnyObject?, forUndefinedKey key: String) {}
    
    //MARK:- 归档 & 解档
    ///归档
    func saveUserAccount() {
        NSKeyedArchiver.archiveRootObject(self, toFile: UserAccount.userSandBoxPath)
    }
    ///解档
    class func loadUserAccount() -> UserAccount? {
        return NSKeyedUnarchiver.unarchiveObjectWithFile(UserAccount.userSandBoxPath) as? UserAccount
    }
    func encodeWithCoder(aCoder: NSCoder) {
        aCoder.encodeObject(uid, forKey: "uid")
        aCoder.encodeObject(idstr, forKey: "idstr")
        aCoder.encodeObject(access_token, forKey: "access_token")
        aCoder.encodeObject(expires_in, forKey: "expores_in")
        aCoder.encodeObject(name, forKey: "name")
        aCoder.encodeObject(profile_image_url, forKey: "profile_image_url")
        aCoder.encodeObject(location, forKey: "location")
        aCoder.encodeObject(created_at, forKey: "created_at")

    }
    required init?(coder aDecoder: NSCoder) {
        aDecoder.decodeObjectForKey("uid")
        aDecoder.decodeObjectForKey("idstr")
        aDecoder.decodeObjectForKey("access_token")
        aDecoder.decodeObjectForKey("expores_in")
        aDecoder.decodeObjectForKey("name")
        aDecoder.decodeObjectForKey("profile_image_url")
        aDecoder.decodeObjectForKey("location")
        aDecoder.decodeObjectForKey("created_at")
    }

}
